package com.jdew.thkj.activity;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Display;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.jaeger.library.StatusBarUtil;
import com.jdew.thkj.R;
import com.jdew.thkj.bean.TokenBean;
import com.jdew.thkj.bean.WelcomeBean;
import com.jdew.thkj.manager.ActivityManagerUtils;
import com.jdew.thkj.manager.AppConfig;
import com.jdew.thkj.network.RetrofitManager;
import com.jdew.thkj.network.api.RequestApi;
import com.jdew.thkj.utils.DeviceUtils;
import com.jdew.thkj.utils.MD5Utils;
import com.jdew.thkj.utils.RegexValidateUtils;
import com.jdew.thkj.utils.ToastUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by cangku on 2017/9/8.
 */

public class WelcomeActivity extends AppCompatActivity{
    @Bind(R.id.welcome)
    ImageView welcome;
    private SharedPreferences sp;
    private SharedPreferences.Editor editor;
    private boolean isFirst = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);
        ButterKnife.bind(this);
        initView();
        ActivityManagerUtils.pushActivity(this);
        StatusBarUtil.setTransparentForImageView(this, null);

    }

    public void initView() {
        sp = getSharedPreferences("user", Activity.MODE_PRIVATE);
        editor = sp.edit();
        isFirst = sp.getBoolean("isFirst", true);
        getToken();
        QiDongApp();
    }

    public void SetLoginTime() {
        String timesamp = RegexValidateUtils.getTime13();
        String numsamp = RegexValidateUtils.getRandom10();
        RetrofitManager.getInstance(AppConfig.BASE_URL).createReq(RequestApi.class).SetLoginTime(sp.getString("userPhone", ""), "0", RegexValidateUtils.getUniquePsuedoID(), timesamp, numsamp, MD5Utils.getMD5(RegexValidateUtils.ASCIISort(timesamp + numsamp + RegexValidateUtils.getUniquePsuedoID() + sp.getString("SignToken", "") + "UserPhone" + sp.getString("userPhone", "") + "AndroidOrIos0")).toUpperCase()).observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.io()).subscribe(new Subscriber<JSONObject>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                Log.d("", e.toString());
            }

            @Override
            public void onNext(JSONObject obj) {

            }
        });
    }

    public void getToken() {
        if (!DeviceUtils.isNetworkAvalible(this)) {
            if (!isFinishing()) {
                ToastUtils.getInstance(WelcomeActivity.this).showMessage("网络忙,请检查网络!");
            }
            return;
        }
        RetrofitManager.getInstance(AppConfig.BASE_URL)
                .createReq(RequestApi.class).GetToken(RegexValidateUtils.getUniquePsuedoID(), RegexValidateUtils.getUniquePsuedoID(), RegexValidateUtils.getTime13(), RegexValidateUtils.getRandom10()).observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.io()).subscribe(new Subscriber<TokenBean>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(TokenBean bean) {
                if (bean.getStatusCode().equals("0")) {
                    String token = bean.getData().getSignToken();
                    editor.putString("SignToken", token);
                    editor.apply();
                }
            }
        });
    }

    public void QiDongApp(){
        Glide.with(WelcomeActivity.this).load("http://tlhbanner.oss-cn-hangzhou.aliyuncs.com/Image/3840ef1dca9014f8609d724e1eed50a0_welcome.jpg").into(welcome);
        new Handler().postDelayed(new Runnable() {
            public void run() {
                if (!isFirst) {
                    if (null != sp.getString("userPhone", "") && !sp.getString("userPhone", "").equals("")) {
                        SetLoginTime();
                    }
                    startActivity(new Intent(WelcomeActivity.this, MainActivity.class));
                } else {
                    Intent in = new Intent(WelcomeActivity.this, GuideActivity.class);
                    startActivity(in);
                }
            }
        }, 1000);
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        ActivityManagerUtils.popActivity(this);
    }
}
